package com.microfch.m;

import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.microfch.ninetypetest.R;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

public class DataCenter {
    public static final int ORDER = 1;
    public static final int RANDOM = 2 ;
    private static DataCenter sInstance;
    public final String TAG = this.getClass().getSimpleName();

    public List<Question> getQuestionList() {
        return questionList;
    }

    public List<Topic> getTopicList() {
        return topicList;
    }

    private List<Question> questionList = new ArrayList<>();
    private List<Topic> topicList = new ArrayList<>();
    private int mCurTopicId = 0;
    private Context mContext;
    private Topic mCurrnetTopic;

    public static DataCenter getInstance() {
        if (sInstance == null) {
            sInstance = new DataCenter();
        }
        return sInstance;
    }
    public void loadSuggestion() {
        Resources res = mContext.getResources();					// 操作资源
        InputStream input = res.openRawResource(R.raw.suggestion);	// 读取资源ID
        Scanner scan = new Scanner(input);						// 实例化Scanner
        mCurrnetTopic = null;
        while (scan.hasNext()) {								// 循环读取
            String str = scan.next();
            if(Topic.isTopic(str)) {//如果是一个性格类型。

                mCurrnetTopic = getTopicByName(str);
                if(mCurrnetTopic != null){
                    Log.d(TAG,"loadSuggestion load topic"+str);
                }
            }else{
                if(mCurrnetTopic == null){
                    mCurrnetTopic = new Topic();
                    Log.d(TAG,"loadSuggestion error load mCurrnetTopic == null");
                }else{
                   // Log.d(TAG,"loadSuggestion load Suggestion"+str);
                }
                mCurrnetTopic.addSuggestion(str);//新增一条建议
            }
        }
        scan.close();											// 关闭输入流
        try {													// 关闭输入流
            input.close() ;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private Topic getTopicByName(String str) {
        for(Topic topic:topicList){
            if(str.lastIndexOf(topic.getName())!= -1){
                return topic;
            }
        }
        return null;
    }

    public void loadData() {
        Resources res = mContext.getResources();					// 操作资源
        InputStream input = res.openRawResource(R.raw.question_180);	// 读取资源ID
        Scanner scan = new Scanner(input);						// 实例化Scanner
        while (scan.hasNext()) {								// 循环读取
            String str = scan.next();
            if(Topic.isTopic(str)) {//如果是一个性格类型。
                mCurrnetTopic = new Topic(Topic.nameFromStr(str));//加入一种性格类型
                mCurTopicId++;
                mCurrnetTopic.setID(mCurTopicId);//设置类型
                topicList.add(mCurrnetTopic);//新增一条话题
            }else{
                if(mCurrnetTopic == null){
                    mCurrnetTopic = new Topic();
                    mCurTopicId++;
                    mCurrnetTopic.setID(mCurTopicId);
                    Log.d(TAG,"loadData error load mCurrnetTopic == null");
                }
                mCurrnetTopic.addQuestion(str);//新增一条问题
            }
        }
        scan.close();											// 关闭输入流
        try {													// 关闭输入流
            input.close() ;
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void setContext(Context context) {
        mContext = context;
    }

    public void initQuestion(int order) {
        if(ORDER == order) {
            initOrderQuestion();
        }
        else if( RANDOM == order) {
            initRandomQuestion();
        }
    }

    private void initRandomQuestion() {
         List<Question> list = new ArrayList<>();
        questionList.clear();
        for (Topic topic:
                topicList) {
            list.addAll(topic.getQuestList());
        }
        while(list.size()>0){
            int index = new Random().nextInt(list.size());
            questionList.add(list.get(index));
            list.remove(index);
        }

        setQuestionListOrder();

    }

    private void setQuestionListOrder() {
        int order = 1;
        for(Question question:questionList){
            question.setOrder(order++);
        }
    }

    private void initOrderQuestion() {
        questionList.clear();
        for (Topic topic:
             topicList) {
            questionList.addAll(topic.getQuestList());
        }
        setQuestionListOrder();
    }

    public String getResult() {
        StringBuffer stringBuffer = new StringBuffer();
        int type =1;
        for (Topic topic :
                topicList) {
            List<Question> question_list = topic.getQuestList();
            stringBuffer.append(type++);
            stringBuffer.append("型人格 ");
            stringBuffer.append(topic.getName());
            stringBuffer.append(": \t");
            int count = 0;
            for (Question question : question_list) {
                if (question.getAnswer()) count++;
            }
            stringBuffer.append(count);
            stringBuffer.append("\n");
        }
        Log.d(TAG,stringBuffer.toString());
        return stringBuffer.toString();
    }


    public String[] getTopics() {
        String [] res = new String[topicList.size()];
        for(int i = 0;i<topicList.size();i++){
            res[i]=topicList.get(i).getName();
        }
        return  res;
    }

}
